<template>
	<view class="login-page">
		<view class="" style="position: fixed;top: 0;">
			<image style="width: 100vw;height: 100vh;" src="https://mp-4884c947-ce09-4502-9aaf-4601e78b1b59.cdn.bspapp.com/bg/bg.jpg"
				mode=""></image>
		</view>
		<!-- 授权登录按钮 -->
		<view class="" style="position: relative;z-index: 99;display: flex;flex-direction: column;align-items: center;top: 200rpx;">
			<image style="width: 200rpx;height: 200rpx;border-radius: 100%;" src="https://mp-4884c947-ce09-4502-9aaf-4601e78b1b59.cdn.bspapp.com/bg/微信图片_20221223204528.jpg" mode=""></image>
			<text decode="true" style="font-weight: bold;font-size: 35rpx;color: #ffffff;margin-top: 30rpx;">小镇APPv1.0</text>
		</view>
		<view class="submit-btn" @tap.stop="logins">
			一键登录
		</view>
		<view @click="back" style="color: #eaeaea;position: relative;z-index: 99;top: 700rpx;">
			随便逛逛
		</view>

		<wx-user-info-modal style="z-index: 99;" v-model="showAuthorizationModal" @updated="updatedUserInfoEvent"></wx-user-info-modal>
	</view>
</template>

<script>
	import WxUserInfoModal from '@/uni_modules/tuniaoui-wx-user-info/components/tuniaoui-wx-user-info/tuniaoui-wx-user-info.vue'

	export default {
		components: {
			WxUserInfoModal
		},
		data() {
			return {
				showAuthorizationModal: false,
			}
		},
		methods: {
			//一键登录按钮
			logins() {
				uni.login({
					provider:'weixin',
					success: (res) => {
						console.log(res.code);
						this.getuserisregister(res.code)
					}
				})
			},
			//查询用户是否注册过||登录
			getuserisregister(e){
				uni.showLoading({title:"加载中"})
				uni.request({
					url:'https://xzapi.knowwl.cn/api/mp_wx_login',
					method:'POST',
					data:{
						code:e
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded', 
					},
					success: (res) => {
						uni.hideLoading()
						console.log(res);
						if (res.data.isregUser==false) {
							this.openAuthorizationModal()
						} else{
							//存储用户信息到本地
							uni.setStorage({
								key:'userinfo',
								data:res.data.userinfo,
							})
							uni.showToast({
								title:"登录成功"
							})
							setTimeout(res=>{
								uni.reLaunch({
									url:'/pages/mine/mine'
								})
							},500)
						}
					}
				})
			},
			//返回
			back() {
				uni.reLaunch({
					url:'/pages/mine/mine'
				})
			},
			// 打开获取用户信息弹框
			openAuthorizationModal() {
				this.showAuthorizationModal = true
			},

			// 获取到的用户信息||注册
			updatedUserInfoEvent(info) {
				var userinfo = info
				uni.showLoading({title:"加载中"})
				
				uni.login({
					provider:'weixin',
					success: (res) => {
						console.log(res.code);
						uni.request({
							url:'https://xzapi.knowwl.cn/api/mp_wx_regUser',
							method:'POST',
							data:{
								code:res.code,
								nickname:userinfo.nickname,
								user_pic:userinfo.avatar,
							},
							header: {
								'content-type': 'application/x-www-form-urlencoded', 
							},
							success: (res) => {
								uni.hideLoading()
								this.showAuthorizationModal = false
								uni.login({
									provider:'weixin',
									success: (res) => {
										console.log(res.code);
										this.getuserisregister(res.code)
									}
								})
								console.log(res);
							}
						})
					}
				})
			},
		}
	}
</script>

<style>
	.login-page {
		width: 100vw;
		height: 100vh;
		display: flex;
		align-items: center;
		/* justify-content: center; */
		flex-direction: column;
	}

	/* 授权按钮 */
	.submit-btn {
		width: 80%;
		background-color: #05C160;
		color: #FFFFFF;
		margin-top: 60rpx;
		border-radius: 10rpx;
		padding: 25rpx;
		font-size: 32rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 30rpx;
		position: relative;
		z-index: 99;
		top: 700rpx;
	}
</style>